<?php
require_once("lib/sqlConnector.php");
function name2Id($username) {
    $result = sqlQuery ("SELECT userid FROM users WHERE username = '%s'",$username);
    if ($result && mysql_num_rows($result) > 0)
    if ($arr = mysql_fetch_row($result)) {
        return $arr[0];
    }
    return false;
}

function id2Name($uid) {
    $result = sqlQuery("SELECT * FROM users WHERE userid = '%s'",$uid);
    if ($arr = mysql_fetch_assoc($result)) {
        return $arr["username"];
    }
    return false;
}

function getName() {
    $uid = $GLOBALS['USERID'];
    $result = sqlQuery("SELECT * FROM users WHERE userid = '%s'",$uid);
    $arr = mysql_fetch_assoc($result);
    return $arr['username'];
}

function getNumberOfPosts($uid = 0) {
    if ($uid = 0) {
        $uid = $GLOBALS['USERID'];
    }
    $result = sqlQuery("SELECT * FROM posts WHERE userid ='%s'",$uid);
    $number = 0;
    if ($result) {
        $number = mysql_num_rows($result);
    }
    return $number;
}

function getAverageHate($uid = 0) {
    if ($uid = 0) {
        $uid = $GLOBALS['USERID'];
    }
    $result = sqlQuery("SELECT rating FROM posts WHERE userid='%s'",$uid);
    $average = 0;
    if ($result) {
        $total = 0;
        while($temp = mysql_fetch_row($result)) {
            $total += $temp[0];
        }
        if ($number = mysql_num_rows($result)) {
            $average= $total/$number;
        }
    }
    return $average;
}
?>